<template>
  <div class="classify">
    <div class="header">
      <span class="left">＜</span>
      <h2>分类</h2>
      <span class="icon iconfont icon-guanbi right"></span>
    </div>
    <div class="content">
      <div class="content-left" ref="conLeft">
        <ul class="wrapper-left">
          <li v-for="(item,index) in leftData" :key="index" :class="{active:currentIndex===index}" @click="active(index)" ref="itemsL">{{item.goods}}</li>
        </ul>
      </div>
      <div class="content-right" ref="conRight">
        <ul class="wrapper-right">
          <li v-for="(item,index) in leftData" :key="index" ref="itemsR">
            <img src="//i8.mifile.cn/v1/a1/d54b1a01-4657-80f7-abd4-cb3c5aab2341!720x360.webp" alt="">
            <h2>
              <span>{{item.goods}}</span>
            </h2>
            <ul class="list">
              <li v-for="(item,index) in arrData" :key="index">
                <img :src="item.img" alt="">
                <p>小米9</p>
              </li>
            </ul>
            <van-button type="default" class="more">查看更多</van-button>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>
<script>
import { homeData } from '@/api/index'
import BScroll from '@better-scroll/core'
export default {
  name: 'Classify',
  components: {
  },
  data () {
    return {
      leftData: [
        { goods: '手机' },
        { goods: '电视' },
        { goods: '电脑' },
        { goods: '家电' },
        { goods: '路由' },
        { goods: '出行' },
        { goods: '智能' },
        { goods: '灯具' },
        { goods: '食品' },
        { goods: '饮料' },
        { goods: '衬衫' },
        { goods: '母婴' }
      ],
      arrData: [],
      bsLeft: null,
      bsRight: null,
      scrTop: 0,
      topsArr: []
    }
  },
  methods: {
    async homeDataReq () {
      const res = await homeData()
      this.arrData = res
      console.log(this.arrData)
    },
    bscrollInit () {
      this.bsLeft = new BScroll(this.$refs.conLeft, {
        probeType: 3,
        pullUpLoad: true,
        click: true
      })
      this.bsRight = new BScroll(this.$refs.conRight, {
        probeType: 3,
        pullUpLoad: true
      })
      this.bsRight.on('scroll', (pos) => {
        this.scrTop = Math.abs(pos.y)
        // console.log(this.scrTop)
        this.leftMove()
      })
      this.topsFn()
    },
    // 获取右边每个li距离顶部的高度
    topsFn () {
      for (let i = 0; i < this.$refs.itemsR.length; i++) {
        this.topsArr.push(this.$refs.itemsR[i].offsetTop)
      }
    },
    // 点击左侧li 右侧对应高度
    active (i) {
      this.bsRight.scrollTo(0, -this.topsArr[i], 300)
    },
    leftMove () {
      const li = this.$refs.itemsL[this.currentIndex]
      this.bsLeft.scrollToElement(li, 300, 0, true)
    }
  },
  created () {
    this.homeDataReq()
  },
  mounted () {
    this.bscrollInit()
  },
  computed: {
    currentIndex () {
      return this.topsArr.findIndex((item, index) => {
        return index === this.topsArr.length - 1 ? index : this.scrTop >= item && this.scrTop < this.topsArr[index + 1]
      })
    }
  },
  beforeRouteLeave (to, from, next) {
    this.bsRight.disable()
    this.bsRight.enable()
    // 解决右边滚动没结束，再次返回页面，导致左边列表回弹
    this.bsRight.scrollToElement(this.$refs.itemsR[this.currentIndex])
    next()
  }
}
</script>

<style lang="less" scoped>

// .classify{
  // border: 0.01rem solid #E4E4E5;
// }

.header{
  width: 3.75rem;
  height: 7vh;
  background: #f2f2f2;
  color: #797878;
  line-height: 0.48rem;
  position: fixed;
  z-index: 999;
  span.left{
    font-size: 0.16rem;
    position: absolute;
    left: 0.2rem;
  }
  h2{
    font-size: 0.16rem;
    text-align: center;
    position: absolute;
    left: 0;
    right: 0;
    margin:  auto;
  }
  span.right{
    font-size: 0.18rem;
    position: absolute;
    right: 0.15rem;
  }
}

.content{
  width: 100%;
  display: flex;
  height: 86vh;
  background: #FAFAFA;
  position: absolute;
  top: 0.48rem;
}

.content-left{
  width: 0.9rem;
  height: 86vh;
  overflow: hidden;
}

ul.wrapper-left{

  li{
    width: 0.85rem;
    height: 0.5rem;
    border-right:0.05rem solid #FAFAFA;
    color: #454749;
    font-size: 0.16rem;
    text-align: center;
    line-height: 0.5rem;
  }
  li.active{
    border-right:0.05rem solid #DF261C;
    color: #DF261C;
  }
}

.content-right{
  flex: 1;
  height: 86vh;
  overflow: hidden;
}

ul.wrapper-right{
  padding-bottom: 25vh;
  &>li{
    text-align: center;
    padding-top: 0.22rem;
    img{
      width: 2.55rem;
    }
    h2{
      width: 0.94rem;
      height: 0.02rem;
      background: #eaeaea;
      position: relative;
      margin: 0 auto;
      margin-bottom: 0.08rem;
      margin-top: 0.14rem;
      span{
        line-height: 100%;
        font-size: 0.16rem;
        color: #0E0A0F;
        background: #fff;
        top: -0.08rem;
        position: absolute;
        width: 0.54rem;
        left: 0;
        right: 0;
        margin: auto;
      }
    }
    ul.list{
      height: 1.96rem;
      width: 2.52rem;
      margin: 0 auto;
      display: flex;
      flex-wrap: wrap;
      align-content: space-evenly;
      li{
        width: 33.33%;
        img{
          width: 0.4rem;
          height: 0.5rem;
        }
        p{
          line-height: 100%;
          margin-top: 0.14rem;
          color: #757575;
          text-align: center;
        }
      }
    }
    .more{
      width: 2.52rem;
      height: 0.4rem;
      background: #F9F5E6;
      color: #AC9D86;
      font-size: 0.16rem;
    }
  }
  &>li:first-of-type{
    padding-top: 0.1rem;
  }
}
</style>
